using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace IRCEngine
{
	public sealed class Prefix : MarshalByRefObject
	{
		private static readonly Regex exNickUserHost = new Regex("^[^!@]+(![^@]+|@.*|![^@]+@.*)$", RegexOptions.Compiled);

		private static readonly Regex exHasUser = new Regex("^[^!@]+![^@]+(@.*)?$", RegexOptions.Compiled);

		private static readonly Regex exHasHost = new Regex("^[^!@]+(![^@]+)?@.*$", RegexOptions.Compiled);

		public String Data { get; private set; }

		public bool NickUserHost
		{
			get
			{
				return exNickUserHost.IsMatch(Data);
			}
		}

		public bool HasUser
		{
			get
			{
				return exHasHost.IsMatch(Data);
			}
		}

		public bool HasHost
		{
			get
			{
				return exHasUser.IsMatch(Data);
			}
		}

		public String User
		{
			get
			{
				string data = Data;
				int start = data.IndexOf('!');
				int stop = data.IndexOf('@');
				if (start == -1)
					return null;
				if (stop == -1)
					stop = data.Length;
				return data.Substring(start + 1, stop - start - 1);
			}
		}

		public String Host
		{
			get
			{
				string data = Data;
				int start = data.IndexOf('@');
				if (start == -1)
					return null;
				return data.Substring(start + 1);
			}
		}

		public String Nick
		{
			get
			{
				string data = Data;
				int stop = Math.Min(data.IndexOf('!'), data.IndexOf('@'));
				if (stop == -1)
					return null;
				return data.Substring(0, stop);
			}
		}

		public Prefix(String prefix)
		{
			Data = prefix;
		}
	}
}
